//
//  MessageManager.swift
//  ezyform
//
//  Created by Hubert on 2022/6/15.
//  Copyright © 2022 Client.Solution Pty Ltd. All rights reserved.
//

import UIKit
import HandyJSON

class MessageManager: NSObject {
    var unreadCount: Int = 0
    static let shared = MessageManager()
    
    // MARK: - 获取未读条数
    func getUnreadCount() {
        let service = NotificationService()
        service.setBlockAfterNetwork { result, _ in
//            if let root = UIApplication.shared.delegate?.window {
//                if let tabVC = root?.rootViewController as? TabBarViewController {
//                    let (msgListTabIndex, _) = tabVC.indexOfViewController(MessageListViewController.self)
//                    if let index = msgListTabIndex {
//                        let vc = tabVC.children[index]
                        if let model = JSONDeserializer<NotificationUnreadModel>.deserializeFrom(dict: result as? [String: Any]) {
                            let unreadCount = Int(model.unread_count) ?? 0
                            APPConfig.loginInfo.unReadNotificationCount = unreadCount
//                            vc.tabBarItem.badgeValue = unreadCount > 0 ? String(unreadCount) : nil
                        }
//                    }
//                }
//            }
        } errorBlock: { _,_ in
            
        }
        service.getNotificationUnReadCount()
    }
//    func cleanUnreadCount(){
//        self.unreadCount = 0
//        if let root = Tool.getKeyWindow() {
//            if let tabVC = root.rootViewController as? UITabBarController {
//                let vc = tabVC.children[3]
//                vc.tabBarItem.badgeValue = nil
//            }
//        }
//    }
}
